package com.online.onlinestudent.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.online.onlinestudent.dto.AjaxResult;
import com.online.onlinestudent.entity.Admin;
import com.online.onlinestudent.entity.Course;
import com.online.onlinestudent.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping
    public AjaxResult getAllCourses() {
        List<Course> courses = courseService.list();
        if (courses == null || courses.isEmpty()) {
            return AjaxResult.fail("未查询到结果！");
        }
        return AjaxResult.success(courses);
    }

    @GetMapping("/{id}")
    public AjaxResult getCourseById(@PathVariable Long id) {
        Course course = courseService.getById(id);
        if (course != null) {
            return AjaxResult.success(course);
        } else {
            return AjaxResult.fail("不存在该课程！");
        }
    }

    @PostMapping
    public AjaxResult saveCourse(@RequestBody Course course) {
        try {
            boolean saved = courseService.save(course);
            if (saved) {
                return AjaxResult.success("插入成功！");
            } else {
                return AjaxResult.fail("插入失败！");
            }
        } catch (Exception e) {
            return AjaxResult.fail("插入失败，已存在该课程！");
        }
    }

    @PutMapping("/{id}")
    public AjaxResult updateCourse(@PathVariable Long id, @RequestBody Course course) {
        course.setId(id);
        try {
            boolean updated = courseService.updateById(course);
            if (updated) {
                return AjaxResult.success("更新成功！");
            } else {
                return AjaxResult.fail("更新失败，请重新输入！");
            }
        } catch (Exception e) {
            return AjaxResult.fail("更新失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteCourseById(@PathVariable Long id) {
        try {
            boolean deleted = courseService.removeById(id);
            if (deleted) {
                return AjaxResult.success("删除成功！");
            } else {
                return AjaxResult.fail("删除失败，不存在该课程！");
            }
        } catch (Exception e) {
            return AjaxResult.fail("删除失败：" + e.getMessage());
        }
    }

    @GetMapping("/page")
    public AjaxResult getCoursesByPage(@RequestParam int current, @RequestParam int size) {
        try {
            Page<Course> page = courseService.page(new Page<>(current, size));
            if (page == null || page.getRecords().isEmpty()) {
                return AjaxResult.fail("未查询到结果！");
            }
            return AjaxResult.success(page);
        } catch (Exception e) {
            return AjaxResult.fail("查询失败：" + e.getMessage());
        }
    }



    /**
     * 根据课程分类id获取课程列表
     */
    @GetMapping("/category/{id}")
    public AjaxResult getCoursesByCategoryId(@PathVariable Long id) {
        List<Course> courses = courseService.getCoursesByCategoryId(id);
        if (courses == null || courses.isEmpty()) {
            return AjaxResult.fail("未查询到结果！");
        }
        return AjaxResult.success(courses);
    }


    @PostMapping("/title")
    public AjaxResult fuzzyQueryByTitle(@RequestBody Course course) {
        String title = course.getTitle();
        if (title == null || title.trim().isEmpty()) {
            return AjaxResult.fail("课程名称不能为空！");
        }
        try {
            List<Course> list = courseService.fuzzyQueryByTitle(title);
            if (list.isEmpty()) {
                return AjaxResult.fail("未查询到结果！");
            }
            return AjaxResult.success(list);
        } catch (Exception e) {
            return AjaxResult.fail("查询失败：" + e.getMessage());
        }
    }
}